package com.huchao.www.controller;

import com.huchao.www.dao.LockMapper;
import com.huchao.www.dao.LoginLogMapper;
import com.huchao.www.exception.MyException;
import com.huchao.www.pojo.AdminInfo;
import com.huchao.www.pojo.LockInfo;
import com.huchao.www.pojo.LoginLog;
import com.huchao.www.service.api.LoginService;
import com.huchao.www.utils.IPUtil;
import com.huchao.www.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Description:
 * @BelongsProject: springMVC
 * @BelongsPackage: com.huchao.www.controller
 * @Author: 亖思
 * @CreateTime: 2022/11/28 17:31
 * @Version: 1.0
 */

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private LockMapper lockMapper;
    @Autowired
    private LoginLogMapper loginLogMapper;

    @GetMapping("/login")
    public String showLogin( Model model) {
        System.out.println("访问自定义login2");
        model.addAttribute("username", "后台");
        return "html/login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(String username, String password, String imageCode, HttpServletRequest request) throws Exception {
        System.out.println(username + password + imageCode);

        LockInfo lockInfo = lockMapper.findByLoginName(username);
        AdminInfo admin = new AdminInfo();
        admin.setLoginName(username);
        admin.setPassword(MD5Util.encode(password+username));

        Date nowDate = new Date();

        LoginLog log = new LoginLog();
        log.setInputLoginName(username);
        log.setClientIp(IPUtil.getIP(request));
        log.setLoginTime(nowDate);

        if (!request.getSession().getAttribute("kaptchaVerifyCode").equals(imageCode)) {
            log.setLoginResult(false);
            loginLogMapper.insertLogInfo(log);
            throw new MyException("验证码错误");
        }
        if (loginService.doLogin(admin,username)) {
            //写入session
            request.getSession().setAttribute("username", username);
            request.getSession().setAttribute("adminSession", "OK");
            log.setLoginResult(true);
            loginLogMapper.insertLogInfo(log);
            return "redirect:/index/index";
        }else if(lockInfo.getLockStatus()==1){
            Long jetLag = nowDate.getTime() - lockInfo.getLockedTime().getTime();
            String exceptionMsg = "账号已锁定---" + jetLag / 1000 + "---秒，请等待180秒解锁后重试";
            log.setLoginResult(false);
            loginLogMapper.insertLogInfo(log);
            throw new MyException(exceptionMsg);
        } else{
            log.setLoginResult(false);
            loginLogMapper.insertLogInfo(log);
            throw new MyException("账号或密码错误，请重新输入");
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        //清空session
        session.invalidate();
        return "redirect:/login/login";
    }
}
